package com.csust.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csust.reggie.common.R;
import com.csust.reggie.domain.Category;
import com.csust.reggie.domain.Setmeal;
import com.csust.reggie.dto.SetmealDto;
import com.csust.reggie.service.ICategoryService;
import com.csust.reggie.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yinweiguang
 * @date 2022-04-03 20:21
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetealController {

    @Autowired
    private ISetmealService setmealService;

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/page")
    public R< Page<SetmealDto>> page(int page, int pageSize,String name){
        log.info("setmeal 分页查询  page = {}, pageSize = {}, name = {}",page,pageSize,name);
        // 构造分页器对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        // 创建查询条件
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(null != name,Setmeal::getName,name);
        // 得到查询结果
        setmealService.page(pageInfo,setmealLambdaQueryWrapper);
        //对象拷贝   忽略records 属性的值不拷贝
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto dishDto = new SetmealDto();

            BeanUtils.copyProperties(item,dishDto);

            Long categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);

            if(category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());
        // 封装dto
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto ){
        log.info("setmeal 添加套餐  setmealDto = {}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("添加套餐成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("更新数据成功");
    }

    // 删除套餐
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        // 删除套餐
        log.info("setmealController  delete ids = {} ",ids);
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    /**
     *  修改套餐状态
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable int status,
                            @RequestParam List<Long> ids) {
        log.info("setmealController  status ids = {}，status = {} ",ids,status);
        setmealService.updateSetmealStatus(status,ids);
        return R.success("修改套餐成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

}
